{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "lesbian-springer",
   "metadata": {},
   "source": [
    "## CLI Tests\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cfa9ccc9",
   "metadata": {},
   "source": [
    "## List resources"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "5fd9e443",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\n",
      "{}\n",
      "{}\n",
      "{}\n",
      "{}\n",
      "{}\n"
     ]
    }
   ],
   "source": [
    "!seldon model load -f ./models/sklearn-iris-gs.yaml\n",
    "!seldon model load -f ./models/tfsimple1.yaml\n",
    "!seldon model load -f ./models/sklearn2.yaml\n",
    "!seldon model load -f ./models/cifar10.yaml\n",
    "!seldon experiment start -f ./experiments/ab-default-model.yaml \n",
    "!seldon pipeline load -f ./pipelines/cifar10.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "8e34a00a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "model\t\tstate\t\treason\r\n",
      "-----\t\t-----\t\t------\r\n",
      "iris\t\tModelAvailable\t\r\n",
      "tfsimple1\tModelAvailable\t\r\n",
      "iris2\t\tModelAvailable\t\r\n",
      "cifar10\t\tModelAvailable\t\r\n"
     ]
    }
   ],
   "source": [
    "!seldon model list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "b7b66dfa",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\r\n",
      "\t\"name\": \"cifar10_1\",\r\n",
      "\t\"versions\": [\r\n",
      "\t\t\"1\"\r\n",
      "\t],\r\n",
      "\t\"platform\": \"tensorflow_savedmodel\",\r\n",
      "\t\"inputs\": [\r\n",
      "\t\t{\r\n",
      "\t\t\t\"name\": \"input_1\",\r\n",
      "\t\t\t\"datatype\": \"FP32\",\r\n",
      "\t\t\t\"shape\": [\r\n",
      "\t\t\t\t-1,\r\n",
      "\t\t\t\t32,\r\n",
      "\t\t\t\t32,\r\n",
      "\t\t\t\t3\r\n",
      "\t\t\t]\r\n",
      "\t\t}\r\n",
      "\t],\r\n",
      "\t\"outputs\": [\r\n",
      "\t\t{\r\n",
      "\t\t\t\"name\": \"fc10\",\r\n",
      "\t\t\t\"datatype\": \"FP32\",\r\n",
      "\t\t\t\"shape\": [\r\n",
      "\t\t\t\t-1,\r\n",
      "\t\t\t\t10\r\n",
      "\t\t\t]\r\n",
      "\t\t}\r\n",
      "\t]\r\n",
      "}\r\n"
     ]
    }
   ],
   "source": [
    "!seldon model metadata cifar10 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "8241d2cf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "server\t\treplicas\tmodels\r\n",
      "------\t\t--------\t------\r\n",
      "mlserver\t1\t\t2\r\n",
      "triton\t\t1\t\t2\r\n"
     ]
    }
   ],
   "source": [
    "!seldon server list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "ba809650",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "experiment\t\tactive\t\r\n",
      "----------\t\t------\t\r\n",
      "experiment-sample\ttrue\r\n"
     ]
    }
   ],
   "source": [
    "!seldon experiment list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "5cc2ca16",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "pipeline\t\tstate\t\treason\r\n",
      "--------\t\t-----\t\t------\r\n",
      "cifar10-production\tPipelineReady\tcreated pipeline\r\n"
     ]
    }
   ],
   "source": [
    "!seldon pipeline list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "c486937c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\n",
      "{}\n",
      "{}\n",
      "{}\n",
      "{}\n",
      "{}\n"
     ]
    }
   ],
   "source": [
    "!seldon model unload iris\n",
    "!seldon model unload tfsimple1\n",
    "!seldon model unload iris2\n",
    "!seldon model unload cifar10\n",
    "!seldon experiment stop experiment-sample\n",
    "!seldon pipeline unload cifar10-production"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5b1925df",
   "metadata": {},
   "source": [
    "### Resource Errors from CLI"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "8b4be911",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "apiVersion: mlops.seldon.io/v1alpha1\r\n",
      "kind: Model\r\n",
      "metadata:\r\n",
      "  name: iris\r\n",
      "spec:\r\n",
      "  storagUri: \"gs://seldon-models/mlserver/iris\"\r\n",
      "  requirements:\r\n",
      "  - sklearn\r\n"
     ]
    }
   ],
   "source": [
    "!cat ./models/error-bad-spec.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "checked-cream",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Error: json: unknown field \"storagUri\"\r\n"
     ]
    }
   ],
   "source": [
    "!seldon model load -f ./models/error-bad-spec.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "cb9f8f62",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "apiVersion: mlops.seldon.io/v1alpha1\r\n",
      "kind: Pipeline\r\n",
      "metadata:\r\n",
      "  name: tfsimple-conditional\r\n",
      "spec:\r\n",
      "  steps:\r\n",
      "  - name: conditional\r\n",
      "  - name: mul10\r\n",
      "    input:\r\n",
      "    - conditional.outputs.OUTPUT0\r\n",
      "    tensorMap:\r\n",
      "      conditional.outputs.OUTPUT0: INPUT\r\n",
      "  - name: add10\r\n",
      "    inputs:\r\n",
      "    - conditional.outputs.OUTPUT1\r\n",
      "    tensorMap:\r\n",
      "      conditional.outputs.OUTPUT1: INPUT\r\n",
      "  output:\r\n",
      "    steps:\r\n",
      "    - mul10\r\n",
      "    - add10\r\n",
      "    stepsJoin: any\r\n"
     ]
    }
   ],
   "source": [
    "!cat ./pipelines/error-bad-spec.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "f229a839",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Error: json: unknown field \"input\"\r\n"
     ]
    }
   ],
   "source": [
    "!seldon pipeline load -f ./pipelines/error-bad-spec.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "e5988e99",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "apiVersion: mlops.seldon.io/v1alpha1\r\n",
      "kind: Experiment\r\n",
      "metadata:\r\n",
      "  name: experiment-sample\r\n",
      "spec:\r\n",
      "  default: iris\r\n",
      "  candidate:\r\n",
      "  - name: iris\r\n",
      "    weight: 50\r\n",
      "  - name: iris2\r\n",
      "    weight: 50\r\n"
     ]
    }
   ],
   "source": [
    "!cat ./experiments/error-bad-spec.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "13058853",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Error: json: unknown field \"candidate\"\r\n"
     ]
    }
   ],
   "source": [
    "!seldon experiment start -f ./experiments/error-bad-spec.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "08c65f5b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "apiVersion: mlops.seldon.io/v1alpha1\r\n",
      "kind: Pipeline\r\n",
      "metadata:\r\n",
      "  name: iris\r\n",
      "spec:\r\n",
      "  steps:\r\n",
      "    - name: iris\r\n",
      "  output:\r\n",
      "    steps:\r\n",
      "    - iris\r\n"
     ]
    }
   ],
   "source": [
    "!cat ./pipelines/error-step-name.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "dfa15efb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Error: rpc error: code = FailedPrecondition desc = pipeline iris must not have a step name with the same name as pipeline name\r\n"
     ]
    }
   ],
   "source": [
    "!seldon pipeline load -f ./pipelines/error-step-name.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "da863b1c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "apiVersion: mlops.seldon.io/v1alpha1\r\n",
      "kind: Pipeline\r\n",
      "metadata:\r\n",
      "  name: iris-pipeline\r\n",
      "spec:\r\n",
      "  steps:\r\n",
      "    - name: first\r\n",
      "    - name: second\r\n",
      "      inputs:\r\n",
      "        -\r\n",
      "  output:\r\n",
      "    steps:\r\n",
      "    - iris\r\n"
     ]
    }
   ],
   "source": [
    "!cat ./pipelines/error-empty-input.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "d5e4ff75",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Error: rpc error: code = FailedPrecondition desc = pipeline iris-pipeline step second has an empty input\r\n"
     ]
    }
   ],
   "source": [
    "!seldon pipeline load -f ./pipelines/error-empty-input.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "6d7b65d5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "apiVersion: mlops.seldon.io/v1alpha1\r\n",
      "kind: Pipeline\r\n",
      "metadata:\r\n",
      "  name: iris-pipeline\r\n",
      "spec:\r\n",
      "  steps:\r\n",
      "    - name: first\r\n",
      "    - name: second\r\n",
      "    - name: third\r\n",
      "      inputs:\r\n",
      "        - first\r\n",
      "      triggers:\r\n",
      "        -\r\n",
      "  output:\r\n",
      "    steps:\r\n",
      "    - iris\r\n"
     ]
    }
   ],
   "source": [
    "!cat ./pipelines/error-empty-trigger.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "e643dd64",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Error: rpc error: code = FailedPrecondition desc = pipeline iris-pipeline step third has an empty trigger\r\n"
     ]
    }
   ],
   "source": [
    "!seldon pipeline load -f ./pipelines/error-empty-trigger.yaml"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9e476335",
   "metadata": {},
   "source": [
    "## Failed scheduling errors from CLI"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "81f6ff20",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Error: rpc error: code = FailedPrecondition desc = failed to schedule model badcapabilities. [failed replica filter RequirementsReplicaFilter for server replica mlserver:0 : model requirements [foobar] replica capabilities [mlserver alibi-detect alibi-explain huggingface lightgbm mlflow python sklearn spark-mlib xgboost] failed replica filter RequirementsReplicaFilter for server replica triton:0 : model requirements [foobar] replica capabilities [triton dali fil onnx openvino python pytorch tensorflow tensorrt]]\r\n"
     ]
    }
   ],
   "source": [
    "!seldon model load -f ./models/error-bad-capabilities.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "a1874007",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\r\n"
     ]
    }
   ],
   "source": [
    "!seldon model unload badcapabilities"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "109cf2e1",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
